﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DoNet.Utils
{
    public class Config
    {
        #region 微信支付域名
        /// <summary>
        /// 微信支付域名
        /// </summary>
        public static string[] wechat_pay_url = {
            "api.mch.weixin.qq.com", //通用域名
            "api2.mch.weixin.qq.com" //(备用域名)见跨城冗灾方案
        };
        /// <summary>
        /// 获取 微信支付域名
        /// </summary>
        /// <returns></returns>
        public static string GetWechatPayUrl()
        {
            string apiUrl = DomainUtil.GetDomainConnect(wechat_pay_url);
            if (string.IsNullOrEmpty(apiUrl))
            {
                Log.Error("Utils—Config—GetWechatPayUrl", "微信支付接口域名无法访问，请检查您的电脑有没有联互联网！");
            }
            return apiUrl;
        }
        #endregion

        #region 威富通支付域名
        /// <summary>
        /// 威富通支付域名
        /// </summary>
        public static string[] swiftpass_pay_url = {
            "pay.swiftpass.cn"
        };
        /// <summary>
        /// 获取 威富通支付域名
        /// </summary>
        /// <returns></returns>
        public static string GetSwiftpassPayUrl()
        {
            string apiUrl = DomainUtil.GetDomainConnect(swiftpass_pay_url);
            if (string.IsNullOrEmpty(apiUrl))
            {
                Log.Error("Utils—Config—GetSwiftpassPayUrl", "威富通支付接口域名无法访问，请检查您的电脑有没有联互联网！");
            }
            return apiUrl;
        }
        #endregion

        #region 京东支付域名
        /// <summary>
        /// 支付域名
        /// </summary>
        public static string[] jd_pay_url = {
            "paygate.jd.com"
        };
        /// <summary>
        /// 获取 京东支付域名
        /// </summary>
        /// <returns></returns>
        public static string GetJdPayUrl()
        {
            string apiUrl = DomainUtil.GetDomainConnect(jd_pay_url);
            if (string.IsNullOrEmpty(apiUrl))
            {
                Log.Error("Utils—Config—GetJdPayUrl", "京东支付接口域名无法访问，请检查您的电脑有没有联互联网！");
            }
            return apiUrl;
        }
        #endregion 京东支付域名

        #region 支付宝支付域名
        /// <summary>
        /// 支付域名
        /// </summary>
        public static string[] alipay_pay_url = {
            "openapi.alipay.com"
        };
        /// <summary>
        /// 获取支付宝支付域名
        /// </summary>
        /// <returns></returns>
        public static string GetAliPayUrl()
        {
            string apiUrl = DomainUtil.GetDomainConnect(alipay_pay_url);
            if (string.IsNullOrEmpty(apiUrl))
            {
                Log.Error("Utils—Config—GetAliPayUrl", "支付宝支付接口域名无法访问，请检查您的电脑有没有联互联网！");
            }
            return apiUrl;
        }
        #endregion 支付宝支付域名

        #region 加载配置文件
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> loadCfg()
        {
            string cfgPath = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "config" + Path.DirectorySeparatorChar + "config.properties";
            Dictionary<string, string> cfg = new Dictionary<string, string>();
            using (StreamReader sr = new StreamReader(cfgPath))
            {
                while (sr.Peek() >= 0)
                {
                    string line = sr.ReadLine();
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }
                    int startInd = line.IndexOf("=");
                    string key = line.Substring(0, startInd);
                    string val = line.Substring(startInd + 1, line.Length - (startInd + 1));
                    if (!cfg.ContainsKey(key))
                    {
                        //参数没有值 就不加载
                        //if (!string.IsNullOrEmpty(val))
                        cfg.Add(key, val);
                    }
                }
            }

            return cfg;
        }

        /// <summary>
        /// 根据参数名 获取参数值
        /// </summary>
        /// <param name="key">参数名</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            string value = "";
            try
            {
                Dictionary<string, string> cfg = loadCfg();
                value = cfg[key].ToString();
            }
            catch (Exception ex)
            {
                Log.Error("Config", "【key：" + key + "】" + ex.ToString());
            }
            return value;
        }

        #endregion
    }
}

